package com.freelance.vn.apps.puzzle.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Vibrator;

import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.freelance.vn.apps.puzzle.model.Constants;
import com.freelance.vn.apps.puzzle.utils.PlayAreaView;
import com.freelance.vn.apps.puzzle.utils.PlayAreaView.PlayAreaProvider;
import com.freelance.vn.apps.puzzle.R;
import com.google.ads.AdRequest;
import com.google.ads.AdView;
public class PuzzleGameMainActivity extends ActivityBase implements OnClickListener ,PlayAreaProvider{
	private Context mContext;
	PlayAreaView mPlayAreaView;
	private int imgId=R.drawable.ice_age1024x768;
	private ProgressDialog mProgressDialog;
	private Vibrator mVibrator;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		Bundle b = getIntent().getExtras();
		if(b!=null){
			imgId =b.getInt(Constants.EX_SELECTED_IMG);
		}
		
		mVibrator=(Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
		setContentView(R.layout.main);
		
		initSlideDrawer();
		mContext=this;
		AdView adView = (AdView) this.findViewById(R.id.adView);
		adView.loadAd(new AdRequest());
		mPlayAreaView=(PlayAreaView)findViewById(R.id.playView);
		mPlayAreaView.setmPlayAreaProvider(this);
		mProgressDialog=new ProgressDialog(this);
		mProgressDialog.setMessage("Loading...");
		mProgressDialog.setIndeterminate(false);
		mProgressDialog.show();
		mLoadGameTask.execute(imgId);
//		ImageButton mHandleControl=(ImageButton)findViewById(R.id.panelHandle);
//		Animation ani=AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
//		ani.setRepeatCount(Animation.INFINITE);
//		ani.setRepeatMode(Animation.REVERSE);
//		mHandleControl.startAnimation(ani);

	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
	}

	@Override
	public void closeOptionsMenu() {
		// TODO Auto-generated method stub
		super.closeOptionsMenu();
	}
	
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		mPlayAreaView.updatePicturePositions();
		if(mProgressDialog!=null){
			mProgressDialog.dismiss();
		}
		System.gc();
		System.runFinalization();
		System.gc();
	}

	private void initSlideDrawer(){
		ImageView panelView =(ImageView)findViewById(R.id.panelView);
		panelView.setImageResource(imgId);
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v==null){
			return;
		}
		int id = (Integer)v.getId();
		switch(id){
		case R.id.btnBack:
		case R.id.btnLogo:
				Log.i("PuzzleGameMainActivity","on back");
				finish();
				break;
		case R.id.btnReplay:
				Log.i("PuzzleGameMainActivity","on Replay");
				replay();
				break;
		case R.id.btnShare:
				share();
				Log.i("PuzzleGameMainActivity","on btn share");
				break;
		case R.id.btnAbout:
				Log.i("PuzzleGameMainActivity","on btn About");
				Intent intent = new Intent(mContext, Instruction.class);
				startActivity(intent);
				break;
		default:
				break;
		
		}
		
	}
	
	
	
	/**
	 * replay game
	 */
	private void replay(){		
			mPlayAreaView.generateRandomPics();
			mPlayAreaView.setIsRandom(true);
			mPlayAreaView.initProperties(this,imgId);
			
	}
	/**
	 * share game by intent
	 */
	private void share(){
		String appUrl="https://play.google.com/store/apps/details?id=com.freelance.vn.apps.puzzle";
//		appUrl="https://play.google.com/store/apps/details?id=com.handlerexploit.tweedle";
		Intent shareItent = new Intent(Intent.ACTION_SEND);
		shareItent.putExtra(Intent.EXTRA_TEXT, appUrl);
		shareItent.setType("text/plain");
		startActivity(Intent.createChooser(shareItent,"share to"));
		
		
	}
	
	private AsyncTask<Integer, Void, Void> mLoadGameTask = new AsyncTask<Integer, Void, Void>(){

		@Override
		protected Void doInBackground(Integer... params) {
			mPlayAreaView.loadData(mContext,params[0]);			
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {			
			mPlayAreaView.invalidate();
			if(mProgressDialog!=null){
				mProgressDialog.dismiss();
			}
		}
		
	};
	@Override
	public void onItemChanged() {
		// TODO Auto-generated method stub
		Log.i("Linhnv","changed");
		
		if(mVibrator!=null){
			mVibrator.vibrate(40);
		}
	}
}
